﻿package plugin.accomplishment
{
    import com.core.*;
    import com.greensock.*;
    import com.sh.game.display.*;
    import consts.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.accomplishment.consts.*;
    import uiresource.Accomplishment.*;
    import view.*;
    import view.mornExtends.*;
    import vo.*;

    public class AccomplishmentCon extends AccomplishmentUI
    {
        private var at:AttributeTipUI;
        private var _cfgAch:Object;
        private var _cfgItem:Object;
        private var _cfgRec:Object;
        private var _cfgdh:Object;
        private var _data:Object;
        private var _baseRoot:String = "png.Accomplishment.";
        private var matrix:Array;
        private var _ac:Accomplishment;
        private var _curTitle:Button;
        private var _curLabel:Label;
        private var isInit:Boolean = false;
        private var _curTitleArr:Array;
        private var _curHf:Object;
        private var cangetArr:Array;
        private var comArr:Array;
        private var dicTitle:Dictionary;
        public var _defaultTitle:int = 0;
        public var _defaultAchId:int = 0;
        public var _showPage:int = 0;
        private var totalPage:int = 0;
        private var curPage:int = 0;
        public var xzview:XunzhangUICON;
        private var title:int;
        private var titleindex:int = 0;
        private var timeline:TweenMax;
        private var _titleArr:Array;
        private var anMc:XMLMovieClip;
        public static const anniuEffect:Class = AccomplishmentCon_anniuEffect;

        public function AccomplishmentCon(param1:Accomplishment)
        {
            this.at = new AttributeTipUI();
            this.matrix = new Array();
            this._curTitleArr = [0, 0];
            this.cangetArr = [];
            this.comArr = [];
            this._ac = param1;
            return;
        }// end function

        override protected function createChildren() : void
        {
            viewClassMap["uiresource.Accomplishment.ItemCtrlUI"] = ItemCtrl;
            loadUI("Accomplishment/Accomplishment.xml");
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            var _loc_1:* = Control.create(AccomplishControl, this);
            ModelProxy.addObserver("achievement", _loc_1);
            this.xzview = new XunzhangUICON();
            this.addChild(this.xzview);
            this.xzview.x = 15;
            this.xzview.y = 73;
            this.xzview.visible = false;
            this.initMessage();
            return;
        }// end function

        private function mcLoaded() : void
        {
            this.anMc = XMLMcManager.instance.getPluginMc("achievement", "chenjiu");
            if (this.anMc)
            {
                this.anMc.mouseEnabled = false;
                this.anMc.mouseChildren = false;
                this.anMc.blendMode = BlendMode.ADD;
                this.anMc.autoPlay = true;
                this.anMc.interval = 1000 / 8;
                this.anMc.x = btn_lqjl.x - 37;
                this.anMc.y = btn_lqjl.y - 19;
                btn_lqjl.parent.addChild(this.anMc);
                this.anMc.visible = !btn_lqjl.disabled;
            }
            return;
        }// end function

        private function initMessage() : void
        {
            if (!this._cfgItem)
            {
                this._cfgItem = ConfigDictionary.data.item;
            }
            if (!this._cfgAch)
            {
                this._cfgAch = ConfigDictionary.data.achievement;
            }
            if (!this._cfgRec)
            {
                this._cfgRec = ConfigDictionary.data.acmRew;
            }
            if (!this._cfgdh)
            {
                this._cfgdh = ConfigDictionary.data.acmdh;
            }
            this.matrix.push(0.3086, 0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0);
            return;
        }// end function

        public function upDateMessage() : void
        {
            this._data = ModelProxy.getValue("data", "achievement");
            if (this.anMc == null)
            {
                XMLMcManager.instance.complatefuncs["achievement"] = this.mcLoaded;
            }
            this.setCh();
            this.initEvent();
            this.initTitle();
            return;
        }// end function

        public function setCount(param1:Boolean = false) : void
        {
            if (param1)
            {
                count2.visible = true;
                count_bg2.visible = true;
                count2.text = "1";
            }
            return;
        }// end function

        private function setCh() : void
        {
            var _loc_1:* = this._cfgdh[this._data.xz];
            txt_cjd.text = "您当前的成就点：" + this._data.res;
            return;
        }// end function

        private function initTitle() : void
        {
            var _loc_1:Object = null;
            var _loc_2:Dictionary = null;
            var _loc_3:Dictionary = null;
            var _loc_4:Dictionary = null;
            var _loc_5:int = 0;
            var _loc_6:Object = null;
            var _loc_7:Object = null;
            this._titleArr = [];
            this.dicTitle = new Dictionary();
            for each (_loc_1 in this._cfgAch)
            {
                
                if (_loc_1.fenlei >= 1)
                {
                    this._titleArr.push(_loc_1);
                }
            }
            this._titleArr.sortOn("id", Array.NUMERIC);
            _loc_2 = new Dictionary();
            _loc_3 = new Dictionary();
            _loc_4 = new Dictionary();
            _loc_5 = 0;
            for each (_loc_6 in this._cfgAch)
            {
                
                _loc_7 = this._data["a" + _loc_6.id];
                if (_loc_7)
                {
                    if (_loc_7[1] == 1)
                    {
                        if (_loc_2[_loc_6.title])
                        {
                            var _loc_10:* = _loc_2;
                            var _loc_11:* = _loc_6.title;
                            var _loc_12:* = _loc_2[_loc_6.title] + 1;
                            _loc_10[_loc_11] = _loc_12;
                        }
                        else
                        {
                            _loc_2[_loc_6.title] = 1;
                        }
                        _loc_5++;
                        continue;
                    }
                    if (_loc_7[1] == 2)
                    {
                        if (_loc_3[_loc_6.title])
                        {
                            var _loc_10:* = _loc_3;
                            var _loc_11:* = _loc_6.title;
                            var _loc_12:* = _loc_3[_loc_6.title] + 1;
                            _loc_10[_loc_11] = _loc_12;
                            continue;
                        }
                        _loc_3[_loc_6.title] = 1;
                    }
                }
            }
            if (_loc_5 > 0)
            {
                btn_lqjl.disabled = false;
                if (this.anMc)
                {
                    this.anMc.visible = true;
                }
                count1.visible = true;
                count_bg1.visible = true;
                count1.text = "" + _loc_5;
            }
            else
            {
                btn_lqjl.disabled = true;
                if (this.anMc)
                {
                    this.anMc.visible = false;
                }
                count1.visible = false;
                count_bg1.visible = false;
            }
            titleList.selectedIndex = this.titleindex;
            this.comArr[tab_select.selectedIndex] = _loc_3;
            this.cangetArr[0] = _loc_2;
            titleList.renderHandler = new Handler(this.titleRender);
            titleList.array = this._titleArr;
            this.setPage();
            return;
        }// end function

        private function titleRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Image = null;
            var _loc_5:Label = null;
            var _loc_6:Label = null;
            var _loc_7:int = 0;
            var _loc_8:Image = null;
            var _loc_9:Dictionary = null;
            var _loc_10:Box = null;
            var _loc_11:Button = null;
            if (param2 < titleList.length)
            {
                _loc_3 = titleList.array[param2];
                _loc_4 = param1.getChildByName("count_bg") as Image;
                _loc_5 = param1.getChildByName("count") as Label;
                _loc_6 = param1.getChildByName("titleName") as Label;
                _loc_6.text = _loc_3.name;
                _loc_7 = this._data["t" + param1.dataSource.title];
                _loc_8 = param1.getChildByName("img_cj") as Image;
                _loc_8.visible = _loc_7 == 1;
                _loc_9 = this.cangetArr[0];
                if (_loc_9[_loc_3.title])
                {
                    _loc_4.visible = true;
                    _loc_5.text = _loc_9[_loc_3.title];
                    _loc_5.visible = true;
                }
                else
                {
                    _loc_4.visible = false;
                    _loc_5.visible = false;
                }
            }
            if (param2 == titleList.selectedIndex)
            {
                this.setSelectTitle(titleList.selectedIndex);
                this.setPage();
            }
            else
            {
                _loc_10 = titleList.getCell(param2);
                _loc_11 = _loc_10.getChildByName("btn_title") as Button;
                _loc_11.selected = false;
            }
            return;
        }// end function

        private function setSelectTitle(param1:int) : Boolean
        {
            var _loc_2:* = titleList.getCell(param1);
            var _loc_3:* = _loc_2.getChildByName("btn_title") as Button;
            if (this._curTitle)
            {
                if (_loc_3 == this._curTitle && _loc_3.selected)
                {
                    return true;
                }
                this._curTitle.selected = false;
            }
            titleList.selectedIndex = param1;
            _loc_3.selected = true;
            this._curTitle = _loc_3;
            return false;
        }// end function

        private function setPage() : void
        {
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            var _loc_1:* = new Array();
            var _loc_2:* = titleList.selectedIndex;
            for each (_loc_3 in this._cfgAch)
            {
                
                if (this._titleArr[_loc_2] && this._titleArr[_loc_2].title)
                {
                    if (_loc_3.title == this._titleArr[_loc_2].title)
                    {
                        _loc_4 = this._data["a" + _loc_3.id];
                        if (_loc_4 == null)
                        {
                            _loc_3.sort = 2;
                        }
                        else
                        {
                            switch(_loc_4[1])
                            {
                                case 0:
                                {
                                    _loc_3.sort = 2;
                                    break;
                                }
                                case 1:
                                {
                                    _loc_3.sort = 1;
                                    break;
                                }
                                case 2:
                                {
                                    _loc_3.sort = 3;
                                    break;
                                }
                                default:
                                {
                                    break;
                                }
                            }
                        }
                        _loc_1.push(_loc_3);
                    }
                }
            }
            _loc_1.sortOn("sort", Array.NUMERIC);
            pList.renderHandler = new Handler(this.pItemRender);
            pList.array = _loc_1;
            this.totalPage = Math.ceil(_loc_1.length / 5);
            this.curPage = 1;
            this.setNumber();
            return;
        }// end function

        private function setNumber() : void
        {
            pList.page = this.curPage - 1;
            page.text = this.curPage + "/" + this.totalPage;
            btn_left.disabled = this.curPage <= 1;
            btn_right.disabled = this.totalPage <= this.curPage;
            return;
        }// end function

        private function pItemRender(param1:ItemCtrl, param2:int) : void
        {
            var curTarget:Object;
            var txt:Label;
            var txt1:Label;
            var itemList:List;
            var ylgimg:Image;
            var curTimes:int;
            var curId:int;
            var goalCount:int;
            var btn:Button;
            var tempA:Object;
            var itemarr:Array;
            var tempItem:Object;
            var goodArr:Array;
            var i:int;
            var rec:Object;
            var msg:Object;
            var item:* = param1;
            var index:* = param2;
            if (index < pList.length)
            {
                curTarget = pList.array[index];
                if (int(curTarget.hexie) == 1)
                {
                    return;
                }
                txt = item.getChildByName("txt_describe") as Label;
                txt1 = item.getChildByName("txt_jindu") as Label;
                itemList = item.getChildByName("itemList") as List;
                ylgimg = item.getChildByName("ydcimg") as Image;
                curTimes;
                curId = curTarget.goalid;
                if (curId == 1)
                {
                    curTimes = UserData.getUserData("level");
                }
                else if (curTarget.goalparam < 0)
                {
                    curTimes = this._data["g" + curId];
                }
                else
                {
                    curTimes = this._data["g" + curId + "_" + pList.array[index].goalparam];
                }
                goalCount = curTarget.goalcount;
                if (curTarget.type == 1)
                {
                    goalCount;
                }
                else
                {
                    curTimes = curTimes < curTarget.goalcount ? (curTimes) : (curTarget.goalcount);
                }
                txt.text = curTarget.desc;
                item.obj = curTarget;
                if (curTarget.plugins)
                {
                    txt.underline = true;
                }
                else
                {
                    txt.underline = false;
                }
                txt1.text = "<font color=\"#ff0000\">(" + curTimes + "/" + goalCount + ")</font>";
                btn = item.getChildByName("btn_get") as Button;
                tempA = this._data["a" + curTarget.id];
                ylgimg.visible = true;
                if (tempA === null)
                {
                    this.setBtn(btn, "未达成", false, true, null);
                    btn.visible = false;
                    ylgimg.url = "png.common.wdc";
                }
                else
                {
                    switch(tempA[1])
                    {
                        case 0:
                        {
                            this.setBtn(btn, "未达成", false, true, null);
                            btn.visible = false;
                            ylgimg.url = "png.common.wdc";
                            break;
                        }
                        case 1:
                        {
                            btn.visible = true;
                            this.setBtn(btn, "可领取", true, false, new Handler(this.getChengJiu, [curTarget.id]));
                            ylgimg.visible = false;
                            txt1.text = "<font color=\"#53b436\">(" + curTimes + "/" + goalCount + ")</font>";
                            break;
                        }
                        case 2:
                        {
                            this.setBtn(btn, "已领取", false, true, null);
                            btn.visible = false;
                            ylgimg.visible = true;
                            ylgimg.url = "png.common.ydc1";
                            txt1.text = "<font color=\"#53b436\">(" + curTimes + "/" + goalCount + ")</font>";
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                }
                itemarr;
                tempItem = ItemObject.create(this._cfgItem[9]);
                tempItem.plus.count = curTarget.amcount;
                itemarr.push(tempItem);
                goodArr = pList.array[index].rewards.split("#");
                var _loc_4:int = 0;
                var _loc_5:* = goodArr;
                while (_loc_5 in _loc_4)
                {
                    
                    i = _loc_5[_loc_4];
                    rec = this._cfgRec[i];
                    msg = new Object();
                    msg = ItemObject.create(this._cfgItem[rec.itemid]);
                    msg.plus.count = rec.count;
                    itemarr.push(msg);
                }
                itemList.array = itemarr;
                itemList.renderHandler = new Handler(function (param1:Component, param2:int) : void
            {
                var _loc_3:* = undefined;
                if (param2 < itemList.array.length)
                {
                    _loc_3 = param1.getChildByName("fi") as FlashItem;
                    _loc_3.data = itemList.array[param2];
                }
                return;
            }// end function
            );
            }
            return;
        }// end function

        private function setBtn(param1:Button, param2:String, param3:Boolean, param4:Boolean, param5:Handler) : void
        {
            param1.label = param2;
            param1.mouseEnabled = param3;
            param1.filters = param4 ? ([new ColorMatrixFilter(this.matrix)]) : (null);
            param1.clickHandler = param5;
            return;
        }// end function

        private function getSelectTitleId() : int
        {
            var _loc_1:* = titleList.selectedIndex;
            if (_loc_1 < 0)
            {
                return 0;
            }
            return _loc_1 > 0 ? (titleList.getCell(_loc_1).dataSource.title) : (titleList.getCell(0).dataSource.title);
        }// end function

        public function initEvent() : void
        {
            btn_close.addEventListener(MouseEvent.CLICK, this.closeWindow);
            titleList.mouseHandler = new Handler(this.changeTitle);
            btn_lqjl.addEventListener(MouseEvent.CLICK, this.getChengHao);
            btn_left.addEventListener(MouseEvent.CLICK, this.onLeft);
            btn_right.addEventListener(MouseEvent.CLICK, this.onRight);
            tab_select.selectHandler = new Handler(this.tabHandler);
            return;
        }// end function

        protected function onRight(event:MouseEvent) : void
        {
            if (this.curPage >= this.totalPage)
            {
                return;
            }
            var _loc_2:String = this;
            var _loc_3:* = this.curPage + 1;
            _loc_2.curPage = _loc_3;
            this.setNumber();
            return;
        }// end function

        protected function onLeft(event:MouseEvent) : void
        {
            if (this.curPage <= 1)
            {
                return;
            }
            var _loc_2:String = this;
            var _loc_3:* = this.curPage - 1;
            _loc_2.curPage = _loc_3;
            this.setNumber();
            return;
        }// end function

        public function tabHandler(param1:int) : void
        {
            tab_select.selectedIndex = param1;
            switch(param1)
            {
                case 0:
                {
                    this.xzview.visible = false;
                    cjview.visible = true;
                    break;
                }
                case 1:
                {
                    this.xzview.visible = true;
                    this.xzview.initLoad();
                    cjview.visible = false;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function removeEvent() : void
        {
            btn_close.removeEventListener(MouseEvent.CLICK, this.closeWindow);
            titleList.mouseHandler = null;
            btn_lqjl.removeEventListener(MouseEvent.CLICK, this.getChengHao);
            return;
        }// end function

        protected function closeWindow(event:MouseEvent) : void
        {
            ModelProxy.change("plugin", "close", "achievement");
            return;
        }// end function

        public function clearMC() : void
        {
            this.removeEvent();
            if (this.anMc)
            {
                this.anMc.dispose();
                if (this.anMc.parent)
                {
                    this.anMc.parent.removeChild(this.anMc);
                }
                this.anMc = null;
            }
            this.xzview.clearMc();
            return;
        }// end function

        private function changeTitle(event:Event, param2:int) : void
        {
            if (event.type == MouseEvent.CLICK)
            {
                if (param2 < 0)
                {
                    return;
                }
                if (this.setSelectTitle(param2))
                {
                    return;
                }
                this.titleindex = param2;
                pList.page = 0;
                this.setPage();
            }
            return;
        }// end function

        private function doDuiHuan(event:MouseEvent) : void
        {
            ModelProxy.change("plugin", "open", {target:"store", data:3});
            return;
        }// end function

        private function getChengHao(event:MouseEvent) : void
        {
            PluginManager.instance.sendToServer("achievement", CMDAchievement.GetAllRewards, this.getSelectTitleId());
            var _loc_2:* = btn_lqjl.localToGlobal(new Point(0, 0));
            ModelProxy.change("data", "itempoint", _loc_2);
            GuideView.getInstance().doGuide(GuideType.GUIDE_CJGB);
            return;
        }// end function

        private function getChengJiu(param1:int) : void
        {
            PluginManager.instance.sendToServer("achievement", CMDAchievement.GetRewards, param1);
            var _loc_2:* = btn_lqjl.localToGlobal(new Point(0, 0));
            ModelProxy.change("data", "itempoint", _loc_2);
            return;
        }// end function

    }
}
